package projorientado.anpr;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import projorientado.image.Segment;
import java.util.Vector;
import projorientado.ocr.RecognizedChar;

/**
 * Representa uma placa de carro.<BR>
 * Uma placa de carro mantém informações sobre seu estado (code), string da placa, custos médio e menor, 
 * informações sobre o reconhecimento além dos segmentos.
 */
public class Plate {

    public static final int
            PLATE_INCOMPLETE_PROCESS = -1,
            PLATE_NO_ERROS = 0,
            PLATE_OCR_FAIL = 1,
            PLATE_NOT_MATCH = 2;
    
    private int code = PLATE_NO_ERROS;
    private float middleCost;
    private float maxCost;
    private String plateString;
    private Vector<Vector<Segment>> blocks;
    private Vector<Vector<RecognizedChar[]>> recognized;

    public BufferedImage toImage(int spaceChars, int spaceBlocks) {
        int height = 0;
        int width = 0;

        for(int i = 0; i < blocks.size(); i++) {
            for(int k = 0; k < blocks.elementAt(i).size(); k++) {
                Segment s = blocks.elementAt(i).elementAt(k);
                if(height < s.getRectangle().height)
                    height = s.getRectangle().height;
                width += s.getRectangle().width+2;
            }
            width += (blocks.elementAt(i).size() - 1)*(spaceChars);
        }
        
        width += (blocks.size() - 1)*spaceBlocks;
        height += 2;
        
        BufferedImage ret = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        
        Graphics g = ret.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);
        
        int x = 0;
        for(int i = 0; i < blocks.size(); i++) {
            for(int k = 0; k < blocks.elementAt(i).size(); k++) {
                if(k != 0)
                    x += spaceChars;
                
                Segment s = blocks.elementAt(i).elementAt(k);
                
                g.setColor(Color.RED);
                g.drawRect(x, 0, s.getRectangle().width+1, s.getRectangle().height+1);
                
                s.getImageLayer().drawBWIntoImage(ret,x+1,1);
                
                x+=s.getRectangle().width + 2;
            }
            x += spaceBlocks;
        }
        
        return ret;
    }
    
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getPlateString() {
        return plateString;
    }

    public void setPlateString(String plateString) {
        this.plateString = plateString;
    }

    public Vector<Vector<Segment>> getBlocks() {
        return blocks;
    }

    public void setBlocks(Vector<Vector<Segment>> blocks) {
        this.blocks = blocks;
    }

    public Vector<Vector<RecognizedChar[]>> getRecognized() {
        return recognized;
    }

    public void setRecognized(Vector<Vector<RecognizedChar[]>> recognized) {
        this.recognized = recognized;
    }

    public float getMiddleCost() {
        return middleCost;
    }

    public void setMiddleCost(float middleCost) {
        this.middleCost = middleCost;
    }

    public float getMaxCost() {
        return maxCost;
    }

    public void setMaxCost(float maxCost) {
        this.maxCost = maxCost;
    }
    
    @Override
    public String toString() {
        return this.getPlateString();
    }
    
}
